package com.four.controller;


import com.four.pojo.Card;
import com.four.pojo.Category;
import com.four.pojo.Product;
import com.four.pojo.Recordscr;
import com.four.service.ICardService;
import com.four.service.ICategoryService;
import com.four.service.IProductService;
import com.four.service.IRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author version v1.0
 * @ClassName：
 * @Description：
 **/
@Controller
public class ProductController {
    @Autowired
    @Qualifier(value = "productService")
    private IProductService iProductService;

    @Autowired
    @Qualifier("categoryService")
    private ICategoryService iCategoryService;
    @Autowired
    @Qualifier("cardServiceImpl")
    private ICardService cardServiceImpl;


    @Autowired
    @Qualifier("recordsService")
    private IRecordsService iRecordsService;


    @RequestMapping("/findAllProduct")
    public String findAllProduct(Model model){
       List<Product> list=iProductService.findAllProduct();
       model.addAttribute("list",list);
       return "goods_List.jsp";
    }
    @RequestMapping("/updateProductPstate")
    public String updateProductPstate(int shelfId,int id){
        Product product=new Product();
        product.setPstate(shelfId);
        product.setPid(id);
        iProductService.updateProductPstate(product);
        return "redirect:findAllProduct";
    }
    @RequestMapping("/addProduct")
    public String addProduct(Product product, Recordscr recordscr){
        product.setStocks(product.getStock());
        iProductService.addProduct(product);


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        Date date = new Date();
        recordscr.setPtime(sdf.format(date));
        iRecordsService.addRecordsRu(recordscr);

        return "redirect:findAllProduct";
    }
    @RequestMapping("/findAllProductCateGory")
    public String findAllProductCateGory(Model model){
        List<Category> list=iCategoryService.findAllCateGory();
        model.addAttribute("list",list);
        return "add_Goods.jsp";
    }
    @RequestMapping("/deleteProduct")
    public String deleteProduct(int id){
        Product product=new Product();
        product.setPid(id);
        iProductService.deleteProduct(id);
        return "redirect:findAllProduct";
    }
    @RequestMapping("/updateProduct")
    public String updateProduct(Product product){
        System.out.println(product.toString());
        iProductService.updateProduct(product);
        return "redirect:findAllProduct";
    }
    @RequestMapping("/findByProductId")
    public String findByProductId(Model model,int pid){
        Product product=iProductService.findByProductId(pid);
        model.addAttribute("product",product);
        List<Category> cateGory=iCategoryService.findAllCateGory();
        model.addAttribute("cateGory",cateGory);
        return "edit_Goods.jsp";
    }
    @RequestMapping("/findProductAll")
    public String findProductAll(Model model,Product product){
        if(product.getPtime()==null){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            String str = "2021-01-01";
            try {
                product.setPtime(sdf.parse(str));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //商品销售报表
        List<Product> productOrders = iProductService.findProductOrders(product);
        model.addAttribute("productOrders",productOrders);
        //财务报表
        List<Product> money = iProductService.findMoney(product);
        Product allMoney = iProductService.findAllMoney(product);
        model.addAttribute("money",money);
        model.addAttribute("allMoney",allMoney);
        //车辆总数
        int member = iProductService.findMember(product);
        model.addAttribute("member",member);
        //年卡/储值卡收入
        Integer nian = iProductService.findNian(product);
        Integer ka = iProductService.findKa(product);
        model.addAttribute("nian",nian);
        model.addAttribute("ka",ka);
        //非会员总收入
        Card card = new Card();
        card.setcTime( product.getPtime());
        card.setEndTime(product.getEndTime());
        int countOrders = cardServiceImpl.findCountOrders(card);
        model.addAttribute("countOrders",countOrders);
        //商品库存报表
        List<Product> list=iProductService.findProductAll(product);
        model.addAttribute("list",list);
        //会员消费
        Integer memberProduce = iProductService.findMemberProduce(product);
        model.addAttribute("memberProduce",memberProduce);
        Integer memberProduces = iProductService.findMemberProduces(product);
        model.addAttribute("memberProduces",memberProduces);
        //售后收入
        Integer after = iProductService.findAfter();
        model.addAttribute("after",after);
        //会员维修收入
        Integer wei = iProductService.findWei();
        model.addAttribute("wei",wei);

        return "sale_Search.jsp";
    }

    @RequestMapping("/findMemberByMid")
    public String findMemberByMid(Model model,String pname){
        List<Product> memberByMid = iProductService.findMemberByMid(pname);
        model.addAttribute("memberByMid",memberByMid);
        return "report_GoodsSale.jsp";
    }
    @RequestMapping("/findMoneyAll")
    public String findMoneyAll(Model model){
        List<Product> moneyAll = iProductService.findMoneyAll();
        model.addAttribute("moneyAll",moneyAll);
        return "report_Nocard.jsp";
    }
    @RequestMapping("/findService")
    public String findService(Model model){
        //非会员详情表
        List<Product> service = iProductService.findService();
        model.addAttribute("service",service);
        return "report_Card.jsp";
    }
    @RequestMapping("/findWei")
    public String findWei(Model model) {
        //会员维修详情表
        List<Product> countWei = iProductService.findCountWei();
        model.addAttribute("countWei",countWei);
        return "report_Nocard2.jsp";
    }

}
